//
//  XY_AdvertisementView_CollectionView.m
//  UICollectionView-封装轮播图
//
//  Created by 岁变 on 16/8/3.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "XY_AdvertisementView_CollectionView.h"
#import "UIImageView+WebCache.h"

@interface XY_AdvertisementView_CollectionView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger count;

@end

@implementation XY_AdvertisementView_CollectionView

static NSString *CollectionViewCellID = @"collectionViewCell";


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createCollectionView];
        [self createPageControl];
        [self createTimer];
    }
    return self;
}

//storyboard中走这个初始化方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        [self createCollectionView];
        [self createPageControl];
        [self createTimer];
    }
    return self;
}


//处理数据,将照片网址数组复制100份放入数据源数组中 用于实现循环轮播效果
- (void)handleDataSoutrce:(NSArray *)arr collectionView:(UICollectionView *)collectionView {
    self.arrURLStr = arr;
    self.arrDataSource = [NSMutableArray array];
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < _arrURLStr.count; j++) {
            [_arrDataSource addObject:@(j)];
        }
    }
    
    [self.collectionView reloadData];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:(50 * _arrURLStr.count + 1) inSection:0] atScrollPosition:(UICollectionViewScrollPositionRight) animated:NO];
    _pageControl.numberOfPages = _arrURLStr.count;
    
}


//创建内容collectionView
- (void)createCollectionView {
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumInteritemSpacing = 0;
    _layout.minimumLineSpacing = 0;
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _layout.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);

    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.frame = self.bounds;

    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[CustomCollectionCell class]
        forCellWithReuseIdentifier:CollectionViewCellID];
    [self addSubview:_collectionView];

}

//创建pagecontrol
- (void)createPageControl {
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
    [_pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    _pageControl.frame = CGRectMake(100, self.frame.size.height - 30, self.frame.size.width - 200, 20);
    [self addSubview:_pageControl];
}

- (void)createTimer {
    self.interval = 5;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_interval target:self selector:@selector(handleUpData:) userInfo:nil repeats:YES];
}


- (void)handlePageControl:(UIPageControl *)pageControl {
    NSInteger index = pageControl.currentPage;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_arrURLStr.count * 50 + index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}

- (void)handleUpData:(NSTimer *)timer {
    NSInteger currentRow = self.collectionView.contentOffset.x / _collectionView.frame.size.width;
    NSInteger currentPage = currentRow % _arrURLStr.count;
    if (currentPage == 0) {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_arrURLStr.count * 50  inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    }
    NSInteger currentRow2 = self.collectionView.contentOffset.x / _collectionView.frame.size.width;

    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:currentRow2 + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    self.pageControl.currentPage = currentPage;
}


//创建标题框
- (void)createTitleLabel {
    
    //self.titleLabel = [UILabel alloc] initWithFrame:<#(CGRect)#>
    
}

#pragma mark ---------协议方法-----------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrDataSource.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellID forIndexPath:indexPath];
    
    NSInteger index = [[_arrDataSource objectAtIndex:indexPath.row] integerValue];
    NSURL *url = [NSURL URLWithString:[_arrURLStr objectAtIndex:index]];
    [cell.picImageView sd_setImageWithURL:url];
    cell.index = index;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCollectionCell *cell = (CustomCollectionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    if ([self.delegate respondsToSelector:@selector(didClickImageAtIndex:scrollView:)]) {
        [self.delegate didClickImageAtIndex:cell.index scrollView:self];
    }
}


//滑动结束时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
   
    NSInteger index = (NSInteger)(self.collectionView.contentOffset.x / self.collectionView.frame.size.width) % _arrURLStr.count;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_arrURLStr.count * 50 + index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
    if ((index - 1) >= 0) {
        _pageControl.currentPage = index - 1;
    } else {
        _pageControl.currentPage = _arrURLStr.count;
    }
}

//开始手动滑动时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //移除计时器
    [self.timer invalidate];
    
}

//结束手动滑动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    //添加计时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:_interval target:self selector:@selector(handleUpData:) userInfo:nil repeats:YES];
}



@end





@interface CustomCollectionCell  ()


@end

@implementation CustomCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.picImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_picImageView];
    }
    return self;
}



@end
